<template>
  <a-modal
    title="编辑用户"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="20">
          <a-col :md="20" :sm="24">
            <a-form-item label="用户姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['userName', {initialValue: user.userName,
                                                  rules: [{required: true, message: '请输入用户姓名', trigger: 'blur'}]}]" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="20">
          <a-col :md="20" :sm="24">
            <a-form-item
              label="手机号码"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-decorator="['phone', {initialValue: user.phone, rules: [
                  {required: true, message: '请输入手机号码', trigger: 'blur'},
                  { validator: validatePhone, trigger: 'blur' }]}]" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { editJudge, validatePhone } from '@/api/basic/user'

export default {
  props: {
    roles: {
      type: Array,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      visible: false,
      confirmLoading: false,
      user: {},

      form: this.$form.createForm(this)
    }
  },
  methods: {
    edit (record) {
      this.visible = true
      this.user = record
    },
    // 前端验证手机号码
    validatePhone (rule, value, callback) {
      const reg = /^[1][3-9][0-9]{9}$/
      if (value === '' || value === undefined || value === null) {
        callback()
      } else {
        if ((!reg.test(value)) && value !== '') {
          callback(new Error('请输入正确的电话号码'))
        } else {
          callback()
        }
      }
    },
    handleSubmit () {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      const that = this
      validateFields((errors, values) => {
        if (!errors) {
          validatePhone({ phone: values.phone }).then(res => {
            editJudge(Object.assign(values, { 'userId': this.user.userId })).then(() => {
              that.visible = false
              that.confirmLoading = false
              that.form.resetFields()
              that.$emit('ok')
            }).catch(err => {
              that.confirmLoading = false
              that.$message.error(err.result || '操作失败！')
            })
          }).catch(() => {
            that.$message.error('该手机号已被注册')
            that.confirmLoading = false
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
